package com.app.twth.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

    /**
     * 字符串是否为空
     *
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        if (str == null || "".equals(str) || "null".equals(str))
            return true;
        else
            return false;
    }

    public static String getString(String str) {
        String verId = "";
        if (!isEmpty(str)) {
            return str;
        }
        return verId;
    }

    public static String int2String(int str) {
        return "" + str;
    }

    public static String getVersionName(Context context) {
        String verId = "";
        try {
            PackageManager packManager = context.getPackageManager();
            PackageInfo info = packManager.getPackageInfo(context.getPackageName(), 0);
            verId = "" + info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return verId;
    }

    public static String getVersionCode(Context context) {
        String verId = "";
        try {
            PackageManager packManager = context.getPackageManager();
            PackageInfo info = packManager.getPackageInfo(context.getPackageName(), 0);
            verId = "" + info.versionCode + "";
            Log.e("PackageManager", "版本号:" + verId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return verId;
    }

    /**
     * 检测WiFi的IP是否输入正确
     *
     * @param addr
     * @return
     */
    public static boolean checkIP(String addr) {
        if (addr.length() < 7 || addr.length() > 15 || "".equals(addr)) {
            return false;
        }
        /**
         * 判断IP格式和范围
         */
        String rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
        Pattern pat = Pattern.compile(rexp);
        Matcher mat = pat.matcher(addr);
        boolean ipAddress = mat.find();
        //============对之前的ip判断的bug在进行判断
        if (ipAddress == true) {
            String ips[] = addr.split("\\.");
            if (ips.length == 4) {
                try {
                    for (String ip : ips) {
                        if (Integer.parseInt(ip) < 0 || Integer.parseInt(ip) > 255) {
                            return false;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            } else {
                return false;
            }
        }
        return ipAddress;
    }

    public static String getStr(String str) {

        if (str == null || "".equals(str))
            return "";
        else
            return str;
    }

    public static String getStr2Int(Integer str) {

        if (str == null)
            return "";
        else
            return str + "";
    }

    public static String getStr2Double(Double str) {

        if (str == null)
            return "";
        else
            return str + "";
    }

    public static String getStr2Int(int str) {
        return str + "";
    }

    public static String getStrNull(String str) {

        if (str == null || "".equals(str))
            return "null";
        else
            return str;
    }

    public static String getStr2IntNull(Integer str) {

        if (str == null)
            return "null";
        else
            return str + "";
    }

    public static boolean isAndroid12() {
        return Build.VERSION.SDK_INT >= 31;
    }

    public static String getValueString(Context context, int strId) {
        return context.getResources().getString(strId);
    }
}
